package com.ity.teamanagement.service.Impl;


import com.ity.teamanagement.code.CommonCode;
import com.ity.teamanagement.entity.inputParam.BackAddTeaMessageParam;
import com.ity.teamanagement.entity.inputParam.BackSearchTeaParam;
import com.ity.teamanagement.entity.inputParam.ScreenParam;
import com.ity.teamanagement.entity.outputResult.*;
import com.ity.teamanagement.exception.ExceptionUtils;
import com.ity.teamanagement.mapper.TeaManagementMapper;
import com.ity.teamanagement.mapper.TeaSearchMapper;
import com.ity.teamanagement.service.TeaSearchService;
import com.ity.teamanagement.utils.PageUtils;
import com.ity.teamanagement.utils.PageableDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author coderFu
 * @create 2021/5/24
 */
@Service
public class TeaSearchServiceImpl implements TeaSearchService {


    @Resource
    private TeaSearchMapper teaSearchMapper;

    @Resource
    private TeaManagementMapper teaManagementMapper;


    @Override
    public PageableDetails findTeaListByCondition(ScreenParam screenParam, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = teaSearchMapper.findTeaListTotalByCondition(screenParam);
        if (total == 0){
            return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<TeaMessageList> teaMessageLists = teaSearchMapper.findTeaListByCondition(screenParam,pageNumber*pageSize,pageSize);
        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),teaMessageLists);
    }

//    @Override
//    public PageableDetails findTeaList(Pageable pageable) {
//        int pageNumber = pageable.getPageNumber();
//        int pageSize = pageable.getPageSize();
//        int total = teaSearchMapper.findAllTeaList();
//        if (total == 0){
//            return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
//        }
//        List<TeaMessageList> teaMessageLists = teaSearchMapper.findTeaList(pageNumber*pageSize,pageSize);
//        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),teaMessageLists);
//    }

    @Override
    public TeaDetailsMessage findTeaDetailsMessageById(Long id) {
        if (id == null){
            ExceptionUtils.error(CommonCode.PARAMETER_ERROR);
        }
        TeaDetailsTempA teaDetailsTempA = teaSearchMapper.findTempTeaMessageA(id);
        if ( teaDetailsTempA == null ){
            return new TeaDetailsMessage();
        }
        TeaDetailsMessage teaDetailsMessage = new TeaDetailsMessage();
        teaDetailsMessage.setId(teaDetailsTempA.getId());
        teaDetailsMessage.setChineseName(teaDetailsTempA.getChineseName());
        teaDetailsMessage.setLatinName(teaDetailsTempA.getLatinName());
        teaDetailsMessage.setGrowYear(teaDetailsTempA.getGrowYear());
        teaDetailsMessage.setCoverPath(teaDetailsTempA.getCoverPath());
        teaDetailsMessage.setAbstractMessage(teaDetailsTempA.getAbstractMessage());
        teaDetailsMessage.setGrade(teaDetailsTempA.getGrade());
        teaDetailsMessage.setProvince(teaDetailsTempA.getProvince());
        teaDetailsMessage.setCity(teaDetailsTempA.getCity());
        teaDetailsMessage.setCounty(teaDetailsTempA.getCounty());
        teaDetailsMessage.setAddress(teaDetailsTempA.getAddress());
        teaDetailsMessage.setVarietyIntroduce(teaDetailsTempA.getVarietyIntroduce());
        teaDetailsMessage.setCreateTime(teaDetailsTempA.getCreateTime());
        teaDetailsMessage.setRareVariety(teaDetailsTempA.getRareVariety());
        teaDetailsMessage.setCoverImgId(teaDetailsTempA.getCoverImgId());
        //添加文章图片
        if (teaDetailsTempA.getAKeyId() !=null){
            List<ArticleImg> articleImgList = teaSearchMapper.findArticleImgByKeyId(teaDetailsTempA.getAKeyId());
           teaDetailsMessage.setArticleImgList(articleImgList);
            System.out.println(teaDetailsMessage);
        }else {
            teaDetailsMessage.setArticleImgList(new ArrayList<>());
            //添加茶种图片
        }
        List<TeaImgVo> teaImgs = teaSearchMapper.findTeaImgListById(id);
        teaDetailsMessage.setTeaImgList(teaImgs);
        return teaDetailsMessage;
    }

    @Override
    public PageableDetails backFindAllTeaListMessageByCondition(Pageable pageable, BackSearchTeaParam backSearchTeaParam) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int total = teaSearchMapper.backFindAllTeaCountByCondition(backSearchTeaParam);
        if (total == 0){
            return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),new ArrayList<>());
        }
        List<BackTeaListMessage> backTeaListMessages = teaSearchMapper.backFindAllTeaListByCondition(backSearchTeaParam,pageNumber*pageSize,pageSize);
        System.out.println(backTeaListMessages);
        return new PageableDetails<>(pageNumber,pageSize,total,returnPages(total,pageSize),backTeaListMessages);
    }

    @Override
    public List<TeaRemarksMessageDetails> findRemarksTeaDetailsMessageById(Long id) {
        //添加附加信息
        List<TeaRemarksMessageDetails> teaDetailsTempB = teaSearchMapper.findRemarkMessageById(id);
        if (teaDetailsTempB == null || teaDetailsTempB.size() ==0){
            return new ArrayList<>();
        }
        LinkedHashMap<Long, TeaRemarksMessageDetails> hashMap = new LinkedHashMap<>();
        LinkedHashMap<Long, List<ExTeaImg>> hashMapImg = new LinkedHashMap<>();
        //初始化 hashMap 确定key 和value
        for (TeaRemarksMessageDetails teaRemarksMessageDetails : teaDetailsTempB) {
            hashMap.put(teaRemarksMessageDetails.getExKeyId(),teaRemarksMessageDetails);
            hashMapImg.put(teaRemarksMessageDetails.getExKeyId(),new ArrayList<ExTeaImg>());
        }
        //通过流获取所有的exKeyId
        List<Long> exKeyIds = teaDetailsTempB.stream().map(TeaRemarksMessageDetails::getExKeyId).collect(Collectors.toList());
        List<ExTeaImg> exTeaImgList = teaSearchMapper.findTeaImgListByIds(exKeyIds);
        //遍历将图片添加图片集合中，将图片集合添加到对应的详情对象中
        for (ExTeaImg exTeaImg : exTeaImgList) {
            List<ExTeaImg> exTeaImgList1 = hashMapImg.get(exTeaImg.getKeyId());
            exTeaImgList1.add(exTeaImg);
            TeaRemarksMessageDetails teaRemarksMessageDetails = hashMap.get(exTeaImg.getKeyId());
            teaRemarksMessageDetails.setExTeaImgList(exTeaImgList1);
        }
        return teaDetailsTempB;
    }


    /**
     * 返回
     *
     * @param pageNumber 当前页
     * @param pageSize   每页条数
     * @param count      总条数
     * @param list       返回数据
     * @param <T>        泛型
     * @return
     */
    public <T> PageableDetails returnPageable(int pageNumber, Integer pageSize, Integer count, List<T> list) {
        return new PageableDetails<>(pageNumber, pageSize, count, list);
    }


    /**
     * 返回总页数
     *
     * @param total
     * @param pageSize
     * @return
     */
    public int returnPages(int total, int pageSize) {
        return PageUtils.returnPages(total, pageSize);
    }
}


